<?php
function YMD2DMY($ymd) {
	$aD = preg_split("@[\-/.]@",'11/12/2012');
	if (count($aD)!=3 || !checkdate($aD[1],$aD[2],$aD[0])) return '';	
	if ($aD[0]<100) $aD[0] = ($aD[0]<70)?$aD[0]+2000:$aD[0]+1900;
	return date("Y-m-d",mktime(0,0,0,$aD[1],$aD[2],$aD[0]));	
}

function DMY2YMD($dmy) {
	$aD = preg_split("@[\-/.]@",'11/12/2012');
	if (count($aD)!=3 || !checkdate($aD[1],$aD[0],$aD[2])) return '';	
	if ($aD[2]<100) $aD[2] = ($aD[2]<70)?$aD[2]+2000:$aD[2]+1900;
	return date("Y-m-d",mktime(0,0,0,$aD[1],$aD[0],$aD[2]));	
}

/**
 * get value of key $aX[$k0][$k1][$k2]...[$k9] o '' if not exists
 * ($k1 ... $k9 optional - ex.: sxVx($aProva,'zxcv',2,'asdf') get $aProva['zxcv'][2]['asdfd'])
 */
function sxVx($aX, $k0, $k1='',$k2='',$k3='',$k4='',$k5='',$k6='',$k7='',$k8='',$k9='',$k10=''
	,$k11='',$k12='',$k13='',$k14='',$k15='',$k16='',$k17='',$k18='',$k19='',$k20='') {
	if (!isset($aX[$k0])) {
		return '';
	}
	if ($k1==='') {
		return $aX[$k0];
	}
	if (!is_array($aX[$k0])) {
		return '';
	}
	return sxVx($aX[$k0],$k1,$k2,$k3,$k4,$k5,$k6,$k7,$k8,$k9,$k10
		,$k11,$k12,$k13,$k14,$k15,$k16,$k17,$k18,$k19,$k20);
}


function serverRedir($url){
	if (strpos($url,'xsn=')===false) {
		if (strpos($url,'?')===false) {
			$url .= "?xsn=".XSN;
		} else {
			$url .= "&xsn=".XSN;
		}
	}
	header("location: $url"); exit();
}

function clientRedir($url){
	$sHTML = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";
	$sHTML .= "<html>\n";
	$sHTML .= "<head>\n";
	$sHTML .= "<meta Name=\"expires\" content= \"Mon, 1 Jan 2000 00:00:00 GMT\" />\n";
	$sHTML .= "<meta http-equiv=\"Pragma\" content=\"no-cache\" />\n";
	$sHTML .= "<meta http-equiv=\"Cache-Control\" content=\"no-cache\" />\n";
	$sHTML .= "<meta http-equiv=\"content-language\" content=\"en\" />\n";
	$sHTML .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
	$sHTML .= "<title>POLARIS - process running ...</title>\n";
	$sHTML .= "</head>\n";
	$sHTML .= "<body onload=\"location.href='$url';\">\n";
	$sHTML .= "process running ...";
	$sHTML .= "</body>\n";
	$sHTML .= "</html>\n";
	echo $sHTML;
	exit();
}
/**
 * crea var REQUEST con valore val se non esiste
 */
function sxRDef($nameVar,$val){
	if (!isset($_REQUEST[$nameVar])) $_REQUEST[$nameVar] = $val;
}

/**
 *  get variable $_REQUEST - no magic-quotes
 */
function sxREQUEST($nameVar){
	return sxREQ($nameVar);
}
function sxREQ($nameVar){
	if (!isset($_REQUEST[$nameVar])) { return ''; }
	return (get_magic_quotes_gpc()) ? stripslashes($_REQUEST[$nameVar]): $_REQUEST[$nameVar];
}

/**
 *  get variable $_REQUEST - no magic-quotes
 */
function sxGET($nameVar){
	if (!isset($_REQUEST[$nameVar])) { return ''; }
	return (get_magic_quotes_gpc()) ? stripslashes($_GET[$nameVar]): $_GET[$nameVar];
}

/**
 *  get variable $_REQUEST - no magic-quotes
 */
function sxPOST($nameVar){
	if (!isset($_REQUEST[$nameVar])) { return ''; }
	return (get_magic_quotes_gpc()) ? stripslashes($_POST[$nameVar]): $_POST[$nameVar];
}

/**
 *  get variable $_COOKIE - no magic-quotes
 */
function sxCOOKIE($nameVar){
	if (!isset($_COOKIE[$nameVar])) { return ''; }
	return (get_magic_quotes_gpc()) ? stripslashes($_COOKIE[$nameVar]): $_COOKIE[$nameVar];
}

/**
 *  get variable no magic-quotes
 */
function sxGPC($var){
	return (get_magic_quotes_gpc())? stripslashes($var) : $var;
}

/**
 *  shortcut htmlentities
 */
function sxHE($var){
	return htmlentities($var);
}
/**
 *  shortcut htmlentities di sxREQ
 */
function sxRHE($name){
	return htmlentities(sxREQ($name));
}
/**
 *  shortcut wordwrap
 */
function sxWW($var,$len=80,$nl="\n"){
	return wordwrap($var,$len,$nl);
}

function sxRWW($name,$len=80,$nl="\n"){
	return wordwrap(sxREQ($name),$len,$nl);
}

/**
 *  sxREQUEST + addslashes (per campi SQL)
 */
function sxAS($val){
	return addslashes($val);
}

/**
 *  sxREQUEST + addslashes (per campi SQL)
 */
function sxRAS($var){
	return addslashes(sxREQ($var));
}

/**
 *  sxREQUEST + intval (per campi SQL)
 */
function sxRInt($var){
	return intval(sxREQ($var));
}

/**
 *  sxREQUEST + trim (per campi SQL)
 */
function sxRTrim($var){
	return trim(sxREQ($var));
}

/**
 * _t - multi-language manegement
 */
function _t($text,$p1='',$p2='',$p3='',$p4='',$p5='',$p6='',$p7='',$p8='') {
	if(function_exists("gettext"))
		return sprintf(gettext($text),$p1,$p2,$p3,$p4,$p5,$p6,$p7,$p8);
	else
		return sprintf($text,$p1,$p2,$p3,$p4,$p5,$p6,$p7,$p8);
}

/**
 * var_dump_ret returns the value of var_dump instead of outputting it.
 */
function var_dump_ret($mixed = null) {
  ob_start();
  var_dump($mixed);
  $content = ob_get_contents();
  ob_end_clean();
  return $content;
}

/**
 * Operation Log recording
 * oper_cod values:
 * 100 = log-in OK
 * 110 = log-in denied
 * 190 = log-out
 * 3xx = insert
 * 4xx = change
 * 5xx = delete
 * 6xx = other
 */
function logRecord($oper_code, $oper_text) {
	global $objSDAO, $aMetaConPar;
	if (!is_object($objSDAO)) $objSDAO = new SDAO($aMetaConPar);
	$idUser = (defined('USEROBJ') and isset($_SESSION[USEROBJ])) ? $_SESSION[USEROBJ]->id : 0;
	list($usec, $sec) = explode(" ", microtime());
	$tsNow = date('Y-m-d H:i:s',$sec).substr($usec,1,7); 
	$sSQL = "INSERT INTO oper_log " .
			"(date_time_in, id_user, oper_code, oper_text) " .
			"VALUES(TIMESTAMP('$tsNow'), $idUser, ".
				intval($oper_code).",'".addslashes(substr($oper_text,0,250))."')";	
	$bRC = $objSDAO->query($sSQL,"insLog",array("contOnErr"=>true));
	//dbg_("log = $oper_code, $oper_text");
	return $bRC;
}

/**
 * Error Log recording
 */
function logError($error_code, $error_text, $opt=array()) {
	global $objSDAO, $aMetaConPar;
	if (!is_object($objSDAO)) $objSDAO = new SDAO($aMetaConPar);
	$idUser = (defined('USEROBJ') and isset($_SESSION[USEROBJ])) ? $_SESSION[USEROBJ]->id : 0;
	list($usec, $sec) = explode(" ", microtime());
	$tsNow = date('Y-m-d H:i:s',$sec).substr($usec,1,7); 
	$sSQL = "INSERT INTO error_log " .
			"(date_time_in, id_user, error_code, error_text) " .
			"VALUES(TIMESTAMP('$tsNow'), $idUser, ".
				intval($error_code).",'".addslashes(substr($error_text,0,250))."')";	
	$bRC = $objSDAO->query($sSQL,"insLog",array("contOnErr"=>true));
	//dbg_("log = $err_code, $err_text");
	if (isset($opt['stopRun']) and $opt['stopRun']===true) {
		die(_t("Terminreqtype anomala - ")." - $err_code - $err_text");
	}
	return $bRC;
}

/**
 * debug message write
 */
function dbg_($mess) {
   global $fh_dbg_;  
   if (!isset($_SESSION[USEROBJ])) { 
   	return false; 
   }
   if (!$_SESSION[USEROBJ]->debug['display']) { 
      return false; 
   }
   if (!$fh_dbg_) {
   	if (!is_dir(SERVER_FS_ROOT."/tmp/")) {
   		@mkdir(SERVER_FS_ROOT."/tmp/",0775);
   	}
      $fh_dbg_ = @fopen(SERVER_FS_ROOT."/tmp/".session_id()."_".SUB_SESSION_NUMBER.".txt","ab");
   }
   if ($fh_dbg_) {
      @fwrite($fh_dbg_, date("H:i:s")." - ".print_r($mess,1)."\r\n");
      return true;
   }
   return false;
}

?>